const IsEqual = require('../template')

console.log("=== IsEqual 性能测试 ===")

// 测试基本类型比较性能
console.log(
  "基本类型比较性能:", (() => {
    const iterations = 1000000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(42, 42)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.001 // 平均每次比较时间应该小于0.001ms
  })()
)

// 测试数组比较性能
console.log(
  "数组比较性能:", (() => {
    const arr1 = Array.from({ length: 1000 }, (_, i) => i)
    const arr2 = Array.from({ length: 1000 }, (_, i) => i)
    const arr3 = Array.from({ length: 1000 }, (_, i) => i + 1)

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(arr1, arr2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次比较时间应该小于0.1ms
  })()
)

// 测试对象比较性能
console.log(
  "对象比较性能:", (() => {
    const obj1 = {}
    const obj2 = {}
    for (let i = 0; i < 1000; i++) {
      obj1[`key${i}`] = i
      obj2[`key${i}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(obj1, obj2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次比较时间应该小于1ms
  })()
)

// 测试深度嵌套比较性能
console.log(
  "深度嵌套比较性能:", (() => {
    let obj1 = { value: 'deep' }
    let obj2 = { value: 'deep' }
    for (let i = 0; i < 20; i++) {
      obj1 = { level: i, nested: obj1 }
      obj2 = { level: i, nested: obj2 }
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(obj1, obj2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 2 // 平均每次比较时间应该小于2ms
  })()
)

// 测试不相等对象比较性能
console.log(
  "不相等对象比较性能:", (() => {
    const obj1 = { a: 1, b: 2, c: 3 }
    const obj2 = { a: 1, b: 2, c: 4 }

    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(obj1, obj2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次比较时间应该小于0.01ms
  })()
)

// 测试循环引用比较性能
console.log(
  "循环引用比较性能:", (() => {
    const obj1 = { a: 1 }
    obj1.self = obj1

    const obj2 = { a: 1 }
    obj2.self = obj2

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        IsEqual.isEqualWithCircular(obj1, obj2)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次比较时间应该小于0.1ms
  })()
)

// 测试自定义比较函数性能
console.log(
  "自定义比较函数性能:", (() => {
    const obj1 = { a: 1, b: 'test' }
    const obj2 = { a: 1, b: 'TEST' }

    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqualWithCustomizer(obj1, obj2, (value1, value2, key) => {
        if (key === 'b') return value1.toLowerCase() === value2.toLowerCase()
        return undefined
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次比较时间应该小于0.01ms
  })()
)

// 测试深度限制比较性能
console.log(
  "深度限制比较性能:", (() => {
    let obj1 = { value: 'deep' }
    let obj2 = { value: 'deep' }
    for (let i = 0; i < 50; i++) {
      obj1 = { level: i, nested: obj1 }
      obj2 = { level: i, nested: obj2 }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqualWithDepth(obj1, obj2, 10)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次比较时间应该小于0.1ms
  })()
)

// 测试属性过滤比较性能
console.log(
  "属性过滤比较性能:", (() => {
    const obj1 = {}
    const obj2 = {}
    for (let i = 0; i < 1000; i++) {
      obj1[`key${i}`] = i
      obj2[`key${i}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqualWithFilter(obj1, obj2, (value, key) => {
        return value % 2 === 0
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次比较时间应该小于1ms
  })()
)

// 测试属性映射比较性能
console.log(
  "属性映射比较性能:", (() => {
    const obj1 = {}
    const obj2 = {}
    for (let i = 0; i < 1000; i++) {
      obj1[`key${i}`] = i
      obj2[`new_key${i}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqualWithMapper(obj1, obj2, (value, key) => {
        return { [`new_${key}`]: value }
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次比较时间应该小于1ms
  })()
)

// 测试验证比较性能
console.log(
  "验证比较性能:", (() => {
    const obj1 = { name: 'John', age: 30, email: 'john@example.com' }
    const obj2 = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = {
      name: { type: 'string', required: true },
      age: { type: 'number', min: 0, max: 150 },
      email: { type: 'string', pattern: /@/ }
    }

    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        IsEqual.isEqualWithValidation(obj1, obj2, schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次比较时间应该小于0.01ms
  })()
)

// 测试统计比较性能
console.log(
  "统计比较性能:", (() => {
    const obj1 = { a: 1, b: { c: 2 }, d: [3, 4] }
    const obj2 = { a: 1, b: { c: 2 }, d: [3, 4] }

    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqualWithStats(obj1, obj2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次比较时间应该小于0.01ms
  })()
)

// 测试异步比较性能
console.log(
  "异步比较性能:", (() => {
    const obj1 = { a: 1, b: 'test' }
    const obj2 = { a: 1, b: 'TEST' }

    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(IsEqual.isEqualAsync(obj1, obj2, async (value1, value2, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        if (key === 'b') return value1.toLowerCase() === value2.toLowerCase()
        return undefined
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => result === true) &&
        avgTime < 5 // 平均每次比较时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试并发比较性能
console.log(
  "并发比较性能:", (() => {
    const obj1 = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const obj2 = { a: 1, b: 2, c: 3, d: 4, e: 5 }

    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(IsEqual.isEqualConcurrent(obj1, obj2, async (value1, value2, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value1 === value2
      }, 3))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => result === true) &&
        avgTime < 10 // 平均每次比较时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试复杂结构比较性能
console.log(
  "复杂结构比较性能:", (() => {
    const obj1 = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }
    const obj2 = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }

    const iterations = 10

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      IsEqual.isEqual(obj1, obj2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 10 // 平均每次比较时间应该小于10ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj1 = { a: 1, b: { c: 2 } }
    const obj2 = { a: 1, b: { c: 2 } }
    const iterations = 10000
    const results = []

    for (let i = 0; i < iterations; i++) {
      const result = IsEqual.isEqual(obj1, obj2)
      results.push(result)
    }

    return results.length === iterations &&
      results.every(result => result === true)
  })()
)

console.log("=== IsEqual 性能测试完成 ===")
